package com.taotao.manage.controller;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.taotao.common.pojo.PicUploadResult;
import com.taotao.manage.service.PropertiesService;

@Controller
@RequestMapping("pic")
public class PicUploadController {
	
	private static final List<String> extensions = Arrays.asList("gif", "png", "jpg", "jpeg", "bmp");
	/*private static final String REPOSITORY_PATH="D:\\project_java\\taotao-img";
	private static final String IMAGE_BASE_URL="http://image.taotao.com";*/
	@Autowired
	private PropertiesService propertiesService;
	
	@RequestMapping(value="upload",method=RequestMethod.POST)
	@ResponseBody
	public PicUploadResult uploadFile(@RequestParam(value="uploadFile",required=false) MultipartFile file) throws Exception{
		
		PicUploadResult result = new PicUploadResult();
		//默认情况下图片不合法
		result.setError(1);
		result.setMessage("图片不合法! ");
		
		try {
			//验证图片的格式
			String extension = StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
			if (!extensions.contains(extension)) {
				//图片格式验证不通过,直接返回
				return result;
			}

			//校验内容是否合法的
			BufferedImage image = ImageIO.read(file.getInputStream());
			if (image == null) {
				return result;
			}

			//校验通过
			result.setError(0);

			//保存
			String filePath = getFilePath(file.getOriginalFilename());
			file.transferTo(new File(filePath));

			//生成图片的绝对引用地址
			String picUrl = StringUtils.replace(StringUtils.substringAfter(filePath, this.propertiesService.REPOSITORY_PATH), "\\", "/");
			
			result.setUrl(this.propertiesService.IMAGE_BASE_URL+picUrl);
			result.setMessage("上传成功");

			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
		
	}
	
	private String getFilePath(String sourceFileName) {
		String baseFolder = this.propertiesService.REPOSITORY_PATH + File.separator + "images";
		Date nowDate = new Date();
		// yyyy/MM/dd
		String fileFolder = baseFolder + File.separator + new DateTime(nowDate).toString("yyyy") + File.separator
				+ new DateTime(nowDate).toString("MM") + File.separator + new DateTime(nowDate).toString("dd");
		File file = new File(fileFolder);
		if (!file.isDirectory()) {
			// 如果目录不存在，则创建目录
			file.mkdirs();
		}
		// 生成新的文件名
		String fileName = new DateTime(nowDate).toString("yyyyMMddhhmmssSSSS") + RandomUtils.nextInt(100, 9999) + "."
				+ StringUtils.substringAfterLast(sourceFileName, ".");
		return fileFolder + File.separator + fileName;
	}

}
